home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / SCRNIO.C < prev    next >
Text File  |  1986-05-18  |  6KB  |  233 lines

  1. /* 1.2  10-08-85                        (scrnio.c)
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1985        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10.  
  11. /*----------------------------------------------------------------------*/
  12.  
  13. GLOBAL int outrow[], outcol[];
  14. GLOBAL tiny HEIGHT, WIDTH;    /* See scrnio.h for definitions.    */
  15. GLOBAL TEXT LEADIN[], RCSEPARATOR[], RCENDER[], CLEARSCREEN[], EREOPG[],
  16.     HOMER[], EREOLN[], NORM_SCRN[], ALT_SCRN[], SCRNINI[], SCRNUNI[];
  17. GLOBAL TBOOL CB4L;
  18. GLOBAL tiny ROFFSET, COFFSET;
  19. GLOBAL TBOOL XLT2ASCII, SCRNWRAP, USELAST;
  20.  
  21. /*----------------------------------------------------------------------*/
  22.  
  23. #define toscrn(a, b)    write(STDOUT, a, b) /* write bchars of string a */
  24. #define TABSTOP        8        /* standard tab stops.        */
  25.  
  26. /************************************************************************/
  27.     VOID
  28. iniscrn()        /* Send initialization sequence to terminal    */
  29.  
  30. /*----------------------------------------------------------------------*/
  31. {
  32.     if (*SCRNINI)
  33.         scrsend(SCRNINI);
  34. }
  35.  
  36. /************************************************************************/
  37.     VOID
  38. uniscrn()        /* Send un-initialization sequence to terminal.    */
  39.  
  40. /*----------------------------------------------------------------------*/
  41. {
  42.     if (*SCRNUNI)
  43.         scrsend(SCRNUNI);
  44. }
  45.  
  46. /*\p********************************************************************/
  47.     VOID
  48. clrscrn()    /* Clear the terminal screen and home the cursor.    */
  49.  
  50. /*----------------------------------------------------------------------*/
  51. {
  52. int i;
  53.     if (*CLEARSCREEN)
  54.         scrsend(CLEARSCREEN);
  55.     else
  56.         eraeop(0, 0);
  57.     outrow[STDOUT] = outcol[STDOUT] = 0;
  58. }
  59.  
  60. /************************************************************************/
  61.     VOID
  62. home()            /* Move cursor to row 0, column 0 of terminal.    */
  63.  
  64. /*----------------------------------------------------------------------*/
  65. {
  66.     if (*HOMER)
  67.     {    scrsend(HOMER);
  68.         outrow[STDOUT] = outcol[STDOUT] = 0;
  69.     }
  70.     else
  71.         cursor(0, 0);
  72. }
  73.  
  74. /************************************************************************/
  75.     BOOL
  76. normscrn()        /* Begin normal-intensity mode.  Return TRUE if
  77.                supported, FALSE if not.            */
  78. /*----------------------------------------------------------------------*/
  79. {
  80.     scrsend(NORM_SCRN);
  81.     return (*NORM_SCRN ISNT NULL);
  82. }
  83.  
  84. /************************************************************************/
  85.     BOOL
  86. altscrn()        /* Begin alternate-intensity mode.  Return TRUE 
  87.                if supported, FALSE if not.            */
  88. /*----------------------------------------------------------------------*/
  89. {
  90.     scrsend(ALT_SCRN);
  91.     return (*ALT_SCRN ISNT NULL);
  92. }
  93. /*\p*********************************************************************/
  94.     BOOL
  95. eraeop(r, c)        /* Erase from row r, col c to end of page. Return
  96.                TRUE, or FALSE if improper r, c.        */
  97. /*----------------------------------------------------------------------*/
  98. {
  99.     int i;
  100.  
  101.     if (NOT cursor(r, c))
  102.         return (FALSE);
  103.  
  104.     if (*EREOPG)
  105.         scrsend(EREOPG);
  106.     else
  107.     {    i = r;
  108.         eraeol(r, c);
  109.         while (++i <= HEIGHT - 1)
  110.             eraeol(i, 0);
  111.     }
  112.     return (cursor(r, c));
  113. }
  114.  
  115. /************************************************************************/
  116.     BOOL
  117. eraeol(r, c)        /* Erase row r from column c to end of line. Return
  118.                TRUE, or FALSE on improper r, c.        */
  119. /*----------------------------------------------------------------------*/
  120. {
  121.     int i;
  122.  
  123.     if (NOT cursor(r, c))
  124.         return (FALSE);
  125.  
  126.     if (*EREOLN)
  127.         scrsend(EREOLN);
  128.     else
  129.     {    i = WIDTH - 1 - c;
  130.         while (i--)
  131.             toscrn(" ", 1);
  132.         if ((r ISNT HEIGHT - 1) OR USELAST)
  133.             toscrn(" ", 1);
  134.         cursor(r, c);
  135.     }
  136.     return (TRUE);
  137. }
  138. /*\p*********************************************************************/
  139.     BOOL
  140. cursor(r, c)        /* Position the cursor at row r, column c.  Return
  141.                TRUE if successful, or FALSE if coordinate 
  142.                addressing error is sensed.            */
  143. /*----------------------------------------------------------------------*/
  144. {
  145.     if ((r > HEIGHT - 1) OR (c > WIDTH - 1) OR (r < 0 AND c < 0))
  146.         return (FALSE);
  147.  
  148.     scrsend(LEADIN);
  149.     CB4L ? coord(c + COFFSET) : coord(r + ROFFSET);
  150.     scrsend(RCSEPARATOR);
  151.     CB4L ? coord(r + ROFFSET) : coord(c + COFFSET);
  152.     scrsend(RCENDER);
  153.     outrow[STDOUT] = r;
  154.     outcol[STDOUT] = c;
  155.     return (TRUE);
  156. }
  157.  
  158. /************************************************************************/
  159.     LOCAL VOID
  160. scrsend(s)        /* Send the string s to the terminal        */
  161.  
  162. /*----------------------------------------------------------------------*/
  163. STRING s;
  164. {
  165.     if (*s)
  166.         toscrn(s, strlen(s));
  167. }
  168.  
  169. /************************************************************************/
  170.     LOCAL VOID
  171. coord(x)        /* Output the coordinate x to the screen.    */
  172.  
  173. /*----------------------------------------------------------------------*/
  174. {
  175.     BUFFER buf[10];
  176.  
  177.     if (XLT2ASCII)
  178.     {    itoa(buf, x, 0);
  179.         if (*buf)
  180.             scrsend(buf);
  181.     }
  182.     else
  183.         toscrn(&x, 1);
  184.  
  185. }
  186. /*\p*********************************************************************/
  187.     METACHAR
  188. putscrn(c)        /* put character c to the screen and update row
  189.                and column values appropriately.        */
  190. /*----------------------------------------------------------------------*/
  191. {
  192.     int i;
  193.     char ch;
  194.  
  195. #ifdef CROUTADD
  196.     if (c IS '\n')
  197.         toscrn("\r", 1);
  198. #endif
  199.     if (NOT USELAST AND outrow[STDOUT] IS (HEIGHT -1)
  200.         AND outcol[STDOUT] IS (WIDTH -1))
  201.         return EOF;
  202.  
  203.     ch = c;
  204.     switch(c)
  205.     {   case '\n':
  206.         outrow[STDOUT]++;
  207.         case '\r':
  208.         outcol[STDOUT] = 0;
  209.         break;
  210.         case '\b':
  211.         if (outcol[STDOUT] > 0)
  212.             --outcol[STDOUT];
  213.             toscrn(&ch, 1);
  214.             return c;
  215.         case '\t':
  216.         i = TABSTOP - (outcol[STDOUT] % TABSTOP);
  217.         while ((SCRNWRAP OR outcol[STDOUT] < WIDTH) AND i--)
  218.         {    toscrn(" ", 1);
  219.             ++outcol[STDOUT];
  220.         }
  221.         break;
  222.         default :
  223.         outcol[STDOUT]++;
  224.     }
  225.     if (SCRNWRAP AND outcol[STDOUT] >= WIDTH)
  226.     {    outrow[STDOUT]++;
  227.         outcol[STDOUT] -= WIDTH;
  228.     }
  229.     if (c ISNT '\t')
  230.         toscrn(&ch, 1);
  231.     return c;
  232. }
  233.